package com.itheima.controller;

import com.github.pagehelper.Page;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@Slf4j
@RestController
//员工的controller层 接受请求 响应数据的
@RequestMapping("/emps")
public class EmpController {
    //一样的 创建员工接口 注入 使用多态根据接口使用实现类对象的方法
    @Autowired
    private EmpService empService;

    @GetMapping
    public Result selectEmp(String name, Short gender,

                            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,

                            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,

                            @RequestParam(defaultValue = "1") Integer page,

                            @RequestParam(defaultValue = "10") Integer pageSize) {

        log.info("正在根据条件查询..........name={},gender={}," +
                "begin={},end={},page={},pageSize={}", name, gender, begin, end, page, pageSize);
        PageBean p = empService.selectEmp(name, gender, begin, end, page, pageSize);
        return Result.success(p);
    }

    @PostMapping
    public Result insertEmp(@RequestBody Emp emp) {
        log.info("正在添加员工............emp={}", emp);
        empService.insertEmp(emp);
        return Result.success();
    }

    @DeleteMapping("/{ids}")
    public Result deleteEmp(@PathVariable List<Integer> ids) {
        log.info("正在删除员工...........ids={}", ids);
        empService.deleteEmp(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result selectByIdEmp(@PathVariable Integer id) {
        log.info("正在根据id查询员工.........编辑回显....id={}", id);
        Emp emp = empService.selectByIdEmp(id);
        return Result.success(emp);
    }

    @PutMapping
    public Result updateEmp(@RequestBody Emp emp) {
        log.info("正在修改员工...........会优先触发根据id查询.emp={}", emp);
        empService.updateEmp(emp);
        return Result.success(emp);
    }
}
